package com.leyou.authAgain.service;


import com.leyou.auth.pojo.UserInfo;
import com.leyou.auth.utils.JwtUtils;
import com.leyou.authAgain.client.UserClient;
import com.leyou.authAgain.config.JwtProperties;
import com.leyou.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@EnableConfigurationProperties(JwtProperties.class)
@Service
public class AuthService {
    @Autowired
    private UserClient userClient;
    @Autowired
    private JwtProperties jwtProperties;



    /**
     * 身份验证
     *  ZOOL网关鉴权
     * @param username 用户名
     * @param password 密码
     * @return {@link ResponseEntity}<{@link Void}>
     */
    public String authentication(String username, String password) {

        //查询用
        User user = this.userClient.queryUser(username, password);
        //判断user
        if (user == null){
            return  null;
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername(user.getUsername());
        userInfo.setId(user.getId());

        //jwt生成token
        try {
            String token = JwtUtils.generateToken(userInfo,jwtProperties.getPrivateKey(),jwtProperties.getExpire());
            return token;
        } catch (Exception e) {
            e.printStackTrace();
        }
        //异常
        return null  ;
    }

}
